package remoteRpc

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/go-base-sms/app/rpc/proto"
	"google.golang.org/grpc"
	"log"
	"sync"
)

var (
	smsRpc       *RemoteSmsRpc
	onceSmsCache sync.Once
)

func GetSmsRpc() *RemoteSmsRpc {
	if smsRpc == nil {
		baseLog.Errorf("smsRpc instance is %v", smsRpc)
	}
	return smsRpc
}

type RemoteSmsRpc struct {
	lock                *sync.RWMutex
	SmsRpcServiceClient proto.SmsRpcServiceClient
}

func InitSmsRpc(rpcAddr string) (*RemoteSmsRpc, error) {
	onceSmsCache.Do(func() {
		conn, err := grpc.Dial(rpcAddr, grpc.WithInsecure())
		if err != nil {
			log.Fatalf("rpc did not connect: %v", err)
		}
		smsRpc = &RemoteSmsRpc{
			SmsRpcServiceClient: proto.NewSmsRpcServiceClient(conn),
		}
	})
	return smsRpc, nil
}
